package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.PropertyFeeOrderTemplateMapper;
import com.caiheng.api.entity.estate.PropertyFeeOrderTemplate;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.entity.dto.FeeListTemplateDto;
import com.caiheng.api.entity.dto.FeeOrderTemplateDto;
import com.caiheng.api.service.estate.PropertyFeeOrderTemplateService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.util.DateUtil;
import com.caiheng.api.util.MathUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 物业费相关费用的模板Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-27
 */
@Service
public class PropertyFeeOrderTemplateServiceImpl extends BaseServiceImpl<PropertyFeeOrderTemplateMapper, PropertyFeeOrderTemplate> implements PropertyFeeOrderTemplateService {

    @Resource
    private  PropertyFeeOrderTemplateMapper feeOrderTemplateMapper;

    @Override
    public PropertyFeeOrderTemplate getTemplate(int roomId, String feeType, int isGarage, String yearNum, String monthNum) {
        QueryWrapper<PropertyFeeOrderTemplate> qwRubbish = new QueryWrapper<>();
        qwRubbish.eq("room_id",roomId);
        qwRubbish.eq("year_num",yearNum);
        qwRubbish.eq("month_num",monthNum);
        qwRubbish.eq("fee_type", feeType);
        qwRubbish.eq("is_garage", isGarage);
        qwRubbish.last("limit 1");
        PropertyFeeOrderTemplate template = feeOrderTemplateMapper.selectOne(qwRubbish);
        return template;
    }

    @Override
    public PropertyFeeOrderTemplate getLastTemplateInfo(int roomId, String feeType, int isGarage) {
        QueryWrapper<PropertyFeeOrderTemplate> qwRubbish = new QueryWrapper<>();
        qwRubbish.eq("room_id",roomId);
        qwRubbish.eq("fee_type", feeType);
        qwRubbish.eq("is_garage", isGarage);
        qwRubbish.orderByDesc("year_num,month_num");
        qwRubbish.last("limit 1");
        PropertyFeeOrderTemplate template = feeOrderTemplateMapper.selectOne(qwRubbish);
        return template;
    }

    @Override
    public List<FeeListTemplateDto> getFeeListTemplates(User user, int roomId,String fee_type, String beginMonth, String endMonth) {
        List<FeeListTemplateDto> list = new ArrayList<>();
        List<String> allMonths = DateUtil.getMonthsByTwoDate(beginMonth,endMonth);

        String[] feeAllTypes = null;// { "wyf" , "car", "water", "eletrict"};
        String[] feeAllTypeNames = null;// { "物业费" , "车位费", "水费", "电费"};
        if(fee_type.equals("water")){
            feeAllTypes = new String[] {"water"};
            feeAllTypeNames = new String[] { "水费"};
        }else if(fee_type.equals("eletrict")){
            feeAllTypes =new String[]  {"eletrict"};
            feeAllTypeNames = new String[] { "电费"};
        }else{
            feeAllTypes = new String[] { "wyf" , "car", "water", "eletrict"};
            feeAllTypeNames = new String[] { "物业费" , "车位费", "水费", "电费"};
        }

        int i = 0;
        for (String feeType : feeAllTypes){
            FeeListTemplateDto templateDto = new FeeListTemplateDto();
            templateDto.setFeeType(feeType);
            templateDto.setFeeTypeName(feeAllTypeNames[i]);
            if(CollUtil.isNotEmpty(allMonths)){
                List<PropertyFeeOrderTemplate> templates = new ArrayList<>();
                for (String month : allMonths){
                    String yearNum = month.split("-")[0];
                    String monthNum = month.split("-")[1];

                    PropertyFeeOrderTemplate template = getTemplate(roomId,feeType,0,yearNum,monthNum);
                    if(template != null){
                        double totalPrice = template.getTotalPrice().doubleValue();

                        if(feeType.equals("wyf")){
                            PropertyFeeOrderTemplate rubbish = getTemplate(roomId,"rubbish",0,yearNum,monthNum);
                            if(rubbish != null){
                                template.setRubbish(rubbish);
                                totalPrice += rubbish.getTotalPrice().doubleValue();

                                //优惠金额
                                if(rubbish.getYouhuiPrice()!=null && rubbish.getYouhuiPrice().doubleValue() > 0){
                                    totalPrice -= rubbish.getYouhuiPrice().doubleValue();
                                }
                            }
                        } else if(feeType.equals("water")){
                            PropertyFeeOrderTemplate jiaya = getTemplate(roomId,"jiaya",0,yearNum,monthNum);
                            if(jiaya!=null){
                                template.setJiaya(jiaya);
                                totalPrice += jiaya.getTotalPrice().doubleValue();
                                //优惠金额
                                if(jiaya.getYouhuiPrice()!=null && jiaya.getYouhuiPrice().doubleValue() > 0){
                                    totalPrice -= jiaya.getYouhuiPrice().doubleValue();
                                }
                            }
                        }
                        //优惠金额
                        if(template.getYouhuiPrice()!=null && template.getYouhuiPrice().doubleValue() > 0){
                            totalPrice -= template.getYouhuiPrice().doubleValue();
                        }

                        template.setShowTotalPrice(new BigDecimal(MathUtil.getDouble(totalPrice)));
                        templates.add(template);
                    }

                    //车库费用
                    PropertyFeeOrderTemplate templateGarage = getTemplate(roomId,feeType,1,yearNum,monthNum);
                    if(templateGarage != null){
                        double totalPrice = templateGarage.getTotalPrice().doubleValue();
                        if(feeType.equals("wyf")){
                            PropertyFeeOrderTemplate rubbish = getTemplate(roomId,"rubbish",1,yearNum,monthNum);
                            if(rubbish!=null){
                                templateGarage.setRubbish(rubbish);
                                totalPrice+=rubbish.getTotalPrice().doubleValue();

                                //优惠金额
                                if(rubbish.getYouhuiPrice()!=null && rubbish.getYouhuiPrice().doubleValue() > 0){
                                    totalPrice -= rubbish.getYouhuiPrice().doubleValue();
                                }

                            }
                        } else if(feeType.equals("water")){
                            PropertyFeeOrderTemplate jiaya = getTemplate(roomId,"jiaya",1,yearNum,monthNum);
                            if(jiaya!=null){
                                templateGarage.setJiaya(jiaya);
                                totalPrice += jiaya.getTotalPrice().doubleValue();
                                //优惠金额
                                if(jiaya.getYouhuiPrice()!=null && jiaya.getYouhuiPrice().doubleValue() > 0){
                                    totalPrice -= jiaya.getYouhuiPrice().doubleValue();
                                }
                            }
                        }
                        //优惠金额
                        if(templateGarage.getYouhuiPrice()!=null && templateGarage.getYouhuiPrice().doubleValue() > 0){
                            totalPrice -= templateGarage.getYouhuiPrice().doubleValue();
                        }
                        templateGarage.setShowTotalPrice(new BigDecimal(MathUtil.getDouble(totalPrice)));
                        templates.add(templateGarage);
                    }
                }
                templateDto.setTemplates(templates);
                if(templates.size() > 0){
                    list.add(templateDto);
                }
            }
            i++;
        }
        return list;
    }
}
